package com.heziguo.testDemo.util.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;
/**
 * 
 * @ClassName:  DButil 
 * @Package com.heziguo.testDemo.util.db    
 * @Description:数据库连接工具类(这里用一句话描述这个类的作用)   
 * @author: 贺子国 
 * @date:   2017年12月4日 下午11:04:24   
 *     
 * @Copyright: 2017 www.heziguo.com Inc. All rights reserved. 
 * 注意：
 */
public class DButil {

	private static final String URL = "jdbc:mysql://localhost:3306/bookstore";
	private static final String NAME = "root";
	private static final String PASSWORD = "root";
	
	private static Connection conn = null;
	//加载驱动，连接数据库
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(URL,NAME,PASSWORD);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public static Connection getConnection(){
		return conn;
	}
	
	@Test
	public void test(){
		try {
			Statement stemt = conn.createStatement();
			ResultSet rs = stemt.executeQuery("select username,age from goddess");
			while(rs.next()){//如果对象中有数据，就会循环打印出来
				System.out.println(rs.getString("username")+","+rs.getInt("age"));
			}
				      
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
